package hz.mobileproject.pda.app;

import android.content.Context;
import android.graphics.Color;
import android.support.multidex.MultiDexApplication;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.caption.netmonitorlibrary.netStateLib.NetStateReceiver;

import org.xutils.BuildConfig;
import org.xutils.x;

import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.NetProvider;
import cn.droidlover.xdroidmvp.net.RequestHandler;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.response.Response_login_user;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.push.mqtt.MqttClientMgr;
import hz.mobileproject.pda.utils.SettingFileUtil;
import okhttp3.CookieJar;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
/**
 * Created by wanglei on 2016/12/31.
 */
public class MainApp extends MultiDexApplication {
    private static Context context;
    public static String mainapp="3304";
    private static MainApp mContext;
    private Response_login_user loginUser;
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        mContext = this;
        Utils.init(this);
        ToastUtils.setBgColor(Color.parseColor("#BB000000"));
        ToastUtils.setMsgColor(Color.parseColor("#ffffff"));
        SettingFileUtil.init();
        //Utils与SettingFileUtil的初始化必须位于最前，且不能颠倒
        MqttClientMgr.getInstance(this).initClient();
        MqttClientMgr.getInstance(this).clientConnect();
        //打开错误日志，保存到sd卡
//        CrashHandlerUtils myCrashHandler = CrashHandlerUtils.getInstance();
//        myCrashHandler.init(getApplicationContext());

        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
        /*开启网络广播监听*/
        NetStateReceiver.registerNetworkStateReceiver(this);
        XApi.registerProvider(new NetProvider() {

            @Override
            public Interceptor[] configInterceptors() {
                return new Interceptor[0];
            }

            @Override
            public void configHttps(OkHttpClient.Builder builder) {

            }

            @Override
            public CookieJar configCookie() {
                return null;
            }

            @Override
            public RequestHandler configHandler() {
                return null;
            }

            @Override
            public long configConnectTimeoutMills() {
                return 0;
            }

            @Override
            public long configReadTimeoutMills() {
                return 0;
            }

            @Override
            public boolean configLogEnable() {
                return true;
            }

            @Override
            public boolean handleError(NetError error) {
                return false;
            }
        });


    }

    public static Context getContext() {
        return context;
    }

    public static MainApp getInstance(){
        return mContext;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        NetStateReceiver.unRegisterNetworkStateReceiver(this);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public Response_login_user getLoginUser() {
        if(loginUser == null){
            loginUser = new Response_login_user();
            loginUser.setCus_number(Integer.valueOf(SPUtils.getInstance("app_login").getString(Constance.SP_CUSNUMBER,"0")));
            loginUser.setDepartment_id(Integer.valueOf(SPUtils.getInstance("app_login").getString(Constance.SP_DEPT_ID,"0")));
            loginUser.setDepartment_name(SPUtils.getInstance("app_login").getString(Constance.SP_DEPT_NAME,""));
            loginUser.setPolice_code(SPUtils.getInstance("app_login").getString(Constance.SP_POLICE_CODE,"0"));
            loginUser.setPolice_name(SPUtils.getInstance("app_login").getString(Constance.SP_POLICE_NAME,""));
            loginUser.setPolice_id(SPUtils.getInstance("app_login").getString(Constance.SP_POLICE_ID,"0"));
        }
        return loginUser;
    }

    public void setLoginUser(Response_login_user loginUser) {
        this.loginUser = loginUser;
    }
}
